package com.example.zhihu.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author t-zhaoheng
 * @since 2022/5/9
 */
public class MapUtil {

  // Map的value值降序排序
  public static <K, V extends Comparable<? super V>> Map<K, V> sortDescend(Map<K, V> map) {
    List<Entry<K, V>> list = new ArrayList<>(map.entrySet());
    Collections.sort(list, new Comparator<Entry<K, V>>() {
      @Override
      public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        int compare = (o1.getValue()).compareTo(o2.getValue());
        return -compare;
      }
    });

    Map<K, V> returnMap = new LinkedHashMap<K, V>();
    for (Map.Entry<K, V> entry : list) {
      returnMap.put(entry.getKey(), entry.getValue());
    }
    return returnMap;
  }

  // Map的value值升序排序
  public static <K, V extends Comparable<? super V>> Map<K, V> sortAscend(Map<K, V> map) {
    List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
      @Override
      public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        int compare = (o1.getValue()).compareTo(o2.getValue());
        return compare;
      }
    });

    Map<K, V> returnMap = new LinkedHashMap<K, V>();
    for (Map.Entry<K, V> entry : list) {
      returnMap.put(entry.getKey(), entry.getValue());
    }
    return returnMap;
  }
}
